<?php

use Illuminate\Support\MessageBag;

class UserController extends BaseController
{

    //Home page View
    public function getIndex()
    {
      return View::make('index');
    }

    //Login User Method
    public function loginAction()
    {
      $errors = new MessageBag();

      if ($old = Input::old("errors"))
      {
          $errors = $old;
      }

      $data = array("errors" => $errors);

      if (Input::server("REQUEST_METHOD") == "POST")
      {
          $validator = Validator::make(Input::all(), array(
              "username" => "required",
              "password" => "required"
          ));

          if ($validator->passes())
          {
              $credentials = array(
                  "username" => Input::get("username"),
                  "password" => Input::get("password")
              );

              if (Auth::attempt($credentials))
                return Redirect::route("game/game");
          }
          
          $data["errors"] = new MessageBag(array("password" => array("Username and/or password invalid.")));

          $data["username"] = Input::get("username");

          return Redirect::route("user/login")->withInput($data);
      }

      return View::make("user/login");
    }

    public function registerAction()
    {
      $errors = new MessageBag();

      if ($old = Input::old("errors"))
      {
          $errors = $old;
      }

      $data = array("errors" => $errors);

      if (Input::server("REQUEST_METHOD") == "POST")
      {
        $validator = Validator::make(Input::all(), array(
          "username" => "required|unique:user",
          'email'    => 'required|unique:user|email',
          "password" => "required"
        ));

        if ($validator->passes())
        {
          $password = Input::get("password");
          $password = Hash::make($password);

          $user = new User();
          $user->username = Input::get("username");
          $user->email    = Input::get("email");
          $user->password = $password;
          $user->save();

          $credentials = array(
              "username" => Input::get("username"),
              "password" => Input::get("password")
          );

          if (Auth::attempt($credentials))
          {
              return Redirect::route("user/profile");
          }
          else
            return Redirect::to('user/login');
        }
        else
        {
          $data["errors"] = $validator->failed();
          return Redirect::route("user/register")->withInput($data)->withErrors($validator);
        }
      }

      return View::make("user/register", $data);
    }

    public function requestAction()
    {
        $data = array("requested" => Input::old("requested"));

        if (Input::server("REQUEST_METHOD") == "POST")
        {
            $validator = Validator::make(Input::all(), array(
                "email" => "required"
            ));

            if ($validator->passes())
            {
                $credentials = array("email" => Input::get("email"));

                Password::remind($credentials,
                    function($message, $user)
                    {
                        $message->from("chris@example.com");
                    }
                );

                $data["requested"] = true;

                return Redirect::route("user/request")
                    ->withInput($data);
            }
        }

        return View::make("user/request", $data);
    }

    public function resetAction()
    {
      $token = "?token=" . Input::get("token");

      $errors = new MessageBag();

      if ($old = Input::old("errors"))
      {
          $errors = $old;
      }

      $data = array(
          "token"  => $token,
          "errors" => $errors
      );

      if (Input::server("REQUEST_METHOD") == "POST")
      {
          $validator = Validator::make(Input::all(), array(
              "email"                 => "required|email",
              "password"              => "required|min:6",
              "password_confirmation" => "required|same:password",
              "token"                 => "required|exists:token,token"
          ));

          if ($validator->passes())
          {
              $credentials = array("email" => Input::get("email"));

              Password::reset($credentials,
                  function($user, $password)
                  {
                      $user->password = Hash::make($password);
                      $user->save();

                      Auth::login($user);        

                      return Redirect::route("user/profile");
                  }
              );
          }

          $data["email"]  = Input::get("email");
          $data["errors"] = $validator->errors();

          return Redirect::to(URL::route("user/reset") . $token)
              ->withInput($data);
      }

      return View::make("user/reset", $data);
    }

    public function profileAction()
    {
        return View::make("user/profile");
    }

    public function logoutAction()
    {
        Auth::logout();
        return Redirect::route("user/login");
    }
}